package com.jackrain.nea.web.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.core.schema.Table;
import com.jackrain.nea.core.schema.TableManager;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ReferenceUtil;
import com.jackrain.nea.web.ObjectSingleCmd;
import com.jackrain.nea.web.face.impl.UserImpl;
import com.jackrain.nea.web.query.QuerySession;
import com.jackrain.nea.web.query.QueryUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@Api(value = "TableQueryController", description = "单对象界面rest")
public class ObjectSingleController {

    @Reference(version = "1",group = "ad")
    private ObjectSingleCmd objectSingleCmd1;

    @Reference(version = "2",group = "cp")
    private ObjectSingleCmd objectSingleCmd2;

    @Reference(version = "2",group = "ps")
    private ObjectSingleCmd objectSingleCmd3;

    @Reference(version = "4",group = "vp")
    private ObjectSingleCmd objectSingleCmd4;

    
    @Autowired
    private PropertiesConf pconf;

    @Reference
    private ObjectSingleCmd exportCmd;

    private String service = "com.jackrain.nea.web.ObjectSingleCmd";


    @ApiOperation(value="获取单对象按钮和关联明细表", notes="{\n" + "        \"tabinlinemode\": \"Y\",\n" + "        \"refcolid\": 1457,\n" + "        \"tableid\": 10101,\n" + "        \"tabrelation\": \"1:1\",\n" + "        \"tablename\": \"AD_TABLESQL\",\n" + "        \"tabledesc\": \"表SQL\"\n" + "      } 其中tabinlinemode 如果未N 则表示对应展示的明细表不可以编辑 refcolid 关联主表字段ID tablename 关联表表名 增加新参数ismaintable 默认为y 代表当前获取的表是主表，如果是明细传入n")
    @RequestMapping(path = "/p/cs/objectTab", method = {RequestMethod.GET, RequestMethod.POST})
    public JSONObject getObjectTab(HttpServletRequest request, @RequestParam(value = "table", required = true) String tablename, @RequestParam(value = "objid", required = true) Long objid,
                                    @RequestParam(value = "ismaintable", required = false,defaultValue = "y") String ismaintable) throws Exception {

        UserImpl userWeb = (UserImpl) request.getSession().getAttribute("user");
        QuerySession qsession = QueryUtils.createQuerySession(userWeb);
        TableManager tm = qsession.getTableManager();

        Table table = tm.getTable(tablename);

        String center  = table.getCategory().getSubSystem().getCenter();
        String[] gv = center.split(":");
        if(gv.length != 2){
            throw new NDSException("center is error");
        }
        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), service,gv[0],gv[1]);
        log.info("edas service->"+service+":"+gv[0]+":"+gv[1]);
   
        Object obj = ((ObjectSingleCmd) o).getObjectTab(userWeb,tablename,objid,ismaintable);
        
        JSONObject result=JSONObject.parseObject(obj.toString(), Feature.OrderedField);
        return result;


    }


    @ApiOperation(value="获取主表字段", notes="objid如果不为空则返回对应数据保存的值如果-1或查询不到则默认值")
    @RequestMapping(path = "/p/cs/getObject", method = {RequestMethod.GET, RequestMethod.POST})
    public JSONObject getObject(HttpServletRequest request, @RequestParam(value = "table", required = true) String tablename,@RequestParam(value = "objid", required = false)  Long objid) throws Exception {


        UserImpl userWeb = (UserImpl) request.getSession().getAttribute("user");
        QuerySession qsession = QueryUtils.createQuerySession(userWeb);
        TableManager tm = qsession.getTableManager();
        Table table = tm.getTable(tablename);

        String center  = table.getCategory().getSubSystem().getCenter();
        String[] gv = center.split(":");
        if(gv.length != 2){
            throw new NDSException("center is error");
        }
        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), service,gv[0],gv[1]);
        log.info("edas service->"+service+":"+gv[0]+":"+gv[1]);
        JSONObject result = ((ObjectSingleCmd) o).getObject(userWeb,tablename,objid);
        return result;


    }


    @ApiOperation(value="获取主表批量修改字段字段", notes="默认修改不包含 UI 字段")
    @RequestMapping(path = "/p/cs/getObjectForUpTmp", method = {RequestMethod.GET, RequestMethod.POST})
    public JSONObject getObjectForUpTmp(HttpServletRequest request, @RequestParam(value = "table", required = true) String tablename) throws Exception {


        UserImpl userWeb = (UserImpl) request.getSession().getAttribute("user");
        QuerySession qsession = QueryUtils.createQuerySession(userWeb);
        TableManager tm = qsession.getTableManager();
        Table table = tm.getTable(tablename);

        String center  = table.getCategory().getSubSystem().getCenter();
        String[] gv = center.split(":");
        if(gv.length != 2){
            throw new NDSException("center is error");
        }
        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), service,gv[0],gv[1]);
        log.info("edas service->"+service+":"+gv[0]+":"+gv[1]);
        JSONObject result = ((ObjectSingleCmd) o).getObjectForUpTmp(userWeb,tablename);
        return result;


    }


    @ApiOperation(value="获取对应明细表内容", notes="" +
            "POST /p/cs/objectTab 接口返回 对应所属的关联表有几个 对应的关联字段是什么 tablename，refcolid 作为本次请求的关键参数。objid是当前主表的记录searchdata 是为了配合明细查询的功能 下拉几行 当前第几页" +
            "参数参考 table=AD_REFBYTABLE&objid=10010&refcolid=88&searchdata={\"column_include_uicontroller\":true,\"range\":10,\"startindex\":0}")
    @RequestMapping(path = "/p/cs/objectTableItem", method = {RequestMethod.GET, RequestMethod.POST})
    public JSONObject getObjectTableItem(HttpServletRequest request, @RequestParam(value = "table", required = true) String tablename, @RequestParam(value = "objid", required = true) Long objid,@RequestParam(value = "refcolid", required = true) int refcolid,@RequestParam(value = "searchdata", required = true) String obj) throws Exception {


        log.debug("query data ->("+ obj+")");

        JSONObject jo=JSON.parseObject(obj);

        JSONObject data = new JSONObject();

        UserImpl userWeb = (UserImpl) request.getSession().getAttribute("user");

        QuerySession qsession = QueryUtils.createQuerySession(userWeb);

        TableManager tm = qsession.getTableManager();

        Table table = tm.getTable(tablename);

        String center  = table.getCategory().getSubSystem().getCenter();
        String[] gv = center.split(":");
        if(gv.length != 2){
            throw new NDSException("center is error");
        }
        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), service,gv[0],gv[1]);
        log.info("edas service->"+service+":"+gv[0]+":"+gv[1]);
        JSONObject result = ((ObjectSingleCmd) o).getObjectTableItem(userWeb,tablename,objid,refcolid,obj);
        return result;

    }



    @ApiOperation(value="获取明细表输入字段", notes="商品数量模式、标准模式、新增模式")
    @RequestMapping(path = "/p/cs/inputForitem", method = {RequestMethod.GET, RequestMethod.POST})
    public JSONObject getInputForitem(HttpServletRequest request, @RequestParam(value = "table", required = true) String tablename,@RequestParam(value = "inlinemode", required = true)  String inlinemode) throws Exception {

        UserImpl userWeb = (UserImpl) request.getSession().getAttribute("user");
        QuerySession qsession = QueryUtils.createQuerySession(userWeb);

        TableManager tm = qsession.getTableManager();
        Table table = tm.getTable(tablename);

        String center  = table.getCategory().getSubSystem().getCenter();
        String[] gv = center.split(":");
        if(gv.length != 2){
            throw new NDSException("center is error");
        }
        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), service,gv[0],gv[1]);
        log.info("edas service->"+service+":"+gv[0]+":"+gv[1]);
        JSONObject result = ((ObjectSingleCmd) o).getInputForitem(userWeb,tablename,inlinemode);
        return result;

    }
    
    @ApiOperation(value="获取明细表单对象字段", notes="objid如果不为空则返回对应数据保存的值如果-1或查询不到则默认值")
    @RequestMapping(path = "/p/cs/itemObj", method = {RequestMethod.GET, RequestMethod.POST})
    public JSONObject getItemObj(HttpServletRequest request, @RequestParam(value = "table", required = true) String tablename,@RequestParam(value = "objid", required = true)  Long objid,@RequestParam(value = "refcolid", required = true)  int refcolid) throws Exception {

        UserImpl userWeb = (UserImpl) request.getSession().getAttribute("user");
        QuerySession qsession = QueryUtils.createQuerySession(userWeb);

        TableManager tm = qsession.getTableManager();
        Table table = tm.getTable(tablename);

        String center  = table.getCategory().getSubSystem().getCenter();
        String[] gv = center.split(":");
        if(gv.length != 2){
            throw new NDSException("center is error");
        }
        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), service,gv[0],gv[1]);
        log.info("edas service->"+service+":"+gv[0]+":"+gv[1]);
        JSONObject result = ((ObjectSingleCmd) o).getItemObj(userWeb,tablename,objid,refcolid);
        return result;

    }


    }
